package com.yyq.adms.usermanage.action;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.yyq.adms.sysmanage.pojo.SysModules;
import com.yyq.adms.usermanage.bpo.SysRPMBpo;
import com.yyq.adms.usermanage.bpo.SysRolesBpo;
import com.yyq.adms.usermanage.pojo.SysModulePerms;
import com.yyq.adms.usermanage.pojo.SysPerm;
import com.yyq.adms.usermanage.pojo.SysRPM;
import com.yyq.adms.usermanage.pojo.SysRoles;
import com.yyq.sos.action.BaseAction;
import com.yyq.sos.constants.ActionType;
import com.yyq.sos.permission.LoginUser;

@Controller("sysRPMAction")
@Scope("prototype")
public class SysRPMAction extends BaseAction {

	private SysRPM po = new SysRPM();
	
	private SysRPMBpo bpo;
	
	private SysRolesBpo rBpo ;

	@Resource(name="sysRolesBpo")
	public void setRBpo(SysRolesBpo rBpo) {
		bpo.setCls(po.getClass());
		this.rBpo = rBpo;
	}

	@Resource(name="sysRPMBpo")
	public void setBpo(SysRPMBpo bpo) {
		this.bpo = bpo;
	}

	public String execute() throws Exception {
		super.setObj(po);
		super.setBpo(bpo);
		return super.execute();
	}

	public String mainList() {
		SysRoles role = (SysRoles) rBpo.findByPk(po.getRoleId());
		this.setFormValue("roleName", role.getRoleCname());
		// 找出模块列表中所有父节点
		LoginUser user = (LoginUser)LoginUser.userThread.get();
		this.setFormValue("modules", bpo.search("from " + SysModules.class.getName() + "  where parentId=0"));
//		if(user.getUserId().intValue()==1)
//		else{
//			String sql = ""; 
//		}
		return "preConfig";
	}

	public String data() {
		//this.setFormValue("modules", bpo.search("from " + SysModules.class.getName() + " where parentId=" + po.getModuleId()));
		String[] cname = (String[])this.getFormValue("moduleCname");
		
System.out.println("moduleCname================>"+cname[0]);		
try {
	System.out.println("moduleCname trans encode================>"+new String(cname[0].getBytes("GBK"),"UTF-8"));
	System.out.println("moduleCname trans encode================>"+new String(cname[0].getBytes("UTF-8"),"GBK"));
} catch (UnsupportedEncodingException e) {
	e.printStackTrace();
}
		this.setFormValue("rpms", bpo.search("from " + SysRPM.class.getName() + " where roleId=" + po.getRoleId()+" and moduleId="+po.getModuleId()));
		//
		List mps = bpo.search("select new SysModulePerms(mp.id,mp.permId,mp.moduleId,p.permCname) from "+SysModulePerms.class.getName()+" mp,"+SysPerm.class.getName() +" p where p.id=mp.permId and mp.moduleId="+po.getModuleId());
		this.setFormValue("mps", mps);
		return "permList";
	}

	public String add() {
		String[] mps = (String[])this.getFormValue("mps");
		boolean flag = bpo.saveRPM(po.getRoleId(),po.getModuleId(),mps);
		if(flag)
			this.setMsg("{processId:\"1\",msg:\"角色授权成功\"}");
		else
			this .setMsg("{processId:\"1\",msg:\"角色授权失败\"}");
		return ActionType.ACTION_MSG;
	}

	public SysRPM getPo() {
		return po;
	}

	public void setPo(SysRPM po) {
		this.po = po;
	}
}
